#include <stdio.h>
#include <math.h>
#include <stdint.h>

float PT1000ReadTemperature(uint8_t msb, uint8_t lsb)
{
    uint32_t data;
    float Rt;
    float Rt0 = 1000;
    float Z1, Z2, Z3, Z4, temp;
    float a = 3.9083e-3;
    float b = -5.775e-7;
    float rploy;

    data = (msb << 8) | lsb;
    data >>= 1;
    Rt = (float)data / 32768.0 * RREF;
    Z1 = -a;
    Z2 = a * a - 4 * b;
    Z3 = 4 * b / Rt0;
    Z4 = 2 * b;

    temp = Z2 + Z3 * Rt;
    temp = (sqrt(temp) + Z1) / Z4;

    if (temp >= 0)
    {
        LOG("PT1000 temperature = %d\n", (int)temp);
        return temp;
    }

    rploy = Rt;
    temp = -242.02;
    temp += 2.2228 * rploy;
    rploy *= Rt;
    temp += 2.5859e-3 * rploy;
    rploy *= Rt;
    temp -= 4.8260e-6 * rploy;
    rploy *= Rt;
    temp -= 2.8183e-9 * rploy;
    rploy *= Rt;
    temp += 1.5243e-12 * rploy;

    LOG("PT1000 temperature = %d\n", (int)temp);
    return temp;
}

int main(int argc, char const *argv[])
{
    uint8_t msb = 0x47;
    uint8_t lsb = 0xF8;
    PT1000ReadTemperature(msb, lsb);
    return 0;
}
